import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue'),
    meta: { hideInMenu: true }
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('../components/layout/index.vue'),
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/dashboard/index.vue'),
        meta: { title: '仪表盘', icon: 'dashboard' }
      },
      {
        path: 'classes',
        name: 'Classes',
        component: () => import('../views/classes/index.vue'),
        meta: { title: '班级管理', icon: 'team' }
      },
      {
        path: 'student',
        name: 'Student',
        component: () => import('../views/student/index.vue'),
        meta: { title: '学生管理', icon: 'user' }
      },
      {
        path: 'subject',
        name: 'Subject',
        component: () => import('../views/subject/index.vue'),
        meta: { title: '科目管理', icon: 'book' }
      },
      {
        path: 'grade',
        name: 'Grade',
        component: () => import('../views/grade/index.vue'),
        meta: { title: '成绩管理', icon: 'bar-chart' }
      },
      {
        path: 'user',
        name: 'User',
        component: () => import('../views/user/index.vue'),
        meta: { title: '用户管理', icon: 'user' }
      },
      {
        path: 'setting',
        name: 'Setting',
        component: () => import('../views/setting/index.vue'),
        meta: { title: '系统设置', icon: 'setting' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path !== '/login' && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router